java.io
package can
be classified as
InputStream
or OutputStream
. They
deal in 8-bit bytes.Reader
and Writer
. They deal in
16-bit (Unicode) characters.InputStream
and OutputStream
are used for binary data. The Reader
and
Writer
classes are used for text.ObjectInputStream
and
ObjectOutputStream
allow you to send a whole
object.//Read and write some text Socket a_socket = new Socket(www.sc.edu, 80); InputStreamReader isr = new InputStreamReader(a_socket.getInputStream()); BufferedReader in = new BufferedReader (isr); PrinterWriter out = new PrinterWriter(a_socket.getOutputStream()); //Read and write some binary data Socket socket_data = new Socket(www.sc.edu, 100); DataInputStream in_data = new DataInputStream(Socket_data.getInputStream()); DataOutputStream out_data = new DataOutputStream(socket_data.getOutputStream());
IOException
.IOException
.abstract int read()
: reads a single byte,
an array, or a subarray of bytes. It returns the bytes
read, the number of bytes read, or -1 if end-of-file has
been reached.read()
: takes the byte array, reads an
array or a subarray of bytes and returns a -1 if the
end-of-file has been reached.skip()
: takes long, skips a specified
number of bytes of input and returns the number of bytes
actually skipped.available()
: returns the number of bytes
that can be read without blocking. Both the input and output
can block threads until the byte is read or written.close()
: closes the input stream to free up
system resources.abstract void write()
:takes an integer byte and writes a single byte.void write()
: takes a byte array and writes an array or subarray of bytes.void flush()
: forces any buffered output to be written.void close()
: closes the stream and frees up system resources.ByteArrayInputStream
is created by passing
it an array. You can then read bytes from it as an
InputStream
.
//This code needs to catch exceptions!!! PipedReader pr = new PipedReader(); PipedWriter pw = null; pw = new PipedWriter(pr); //write pw.write("hello"); //read char [] buffer = new char[1000]; int length = 0; length = pr.read(buffer); String output =new String(buffer, 0, length); System.out.println("String is "); System.out.println(output);
//Read BufferedReader input = new BufferedReader (new InputStreamReader(System.in)); //Write Writer out= newBufferedWriter(new FileWriter("file.out")); //Write a system-dependent newline. DANGER!!! out.newLine();
PushBackInputStream
implements a
one-byte pushback with its unread()
method.LineNumberReader
keeps track of the
number of lines read. You can get this number with
getLineNumber()
.PrintStream
and
PrintWriter
use buffering. They flush on a
println
(with automatic flushing turned on)
or a flush
.FileOutputputStream os = new FileOutputStream("Temp1.tmp"); //CDC32 extends Checksum. CRC32 crc32 = new CRC32(); CheckedOutputStream cos = new CheckedOutputStream(os, crc32); cos.write(1); cos.write(2); //Get the checksum for this file long crc = crc32.getValue(); //Write this checksum to the stream. cos.write(crc);
import java.util.zip.*; import java.io.*; class ZipInputStreamExample { public static void main(String argv[]) { try { FileInputStream fis = new FileInputStream("test.zip"); ZipInputStream zis = new ZipInputStream(fis); ZipEntry ze; // Get the first entry ze = zis.getNextEntry(); if (ze == null) System.out.println("End entries"); else { // Use it as stream or reader DataInputStream dis = new DataInputStream(zis); int i = dis.readInt(); System.out.println("File contains " + i); zis.closeEntry(); } zis.close(); } catch(Exception e) { System.out.println(e); } } }
int
, could be
represented in different ways on different machines. This is a
problem.DataInput
and DataOutput
write
data in a platform-independent form.writeUTF
.import java.io.*; class DataInputOutputExample { static BufferedReader system_in = new BufferedReader (new InputStreamReader(System.in)); public static void main(String argv[]) { // Create it { try { FileOutputStream fos = new FileOutputStream("data.dat"); DataOutputStream dos = new DataOutputStream(fos); // Read in three hotels for (int i = 0; i < 3; i++) { Hotel a_hotel = new Hotel(); a_hotel.input(system_in); a_hotel.write_to_dos(dos); } dos.close(); } catch(IOException e) { System.err.println(e); } } // Now read it { try { FileInputStream fis = new FileInputStream("data.dat"); DataInputStream dis = new DataInputStream(fis); Hotel a_hotel = new Hotel(); while (a_hotel.read_from_dis(dis)) { a_hotel.debug_print(); } dis.close(); } catch(IOException e) { System.err.println(e); } } } } class Hotel { private String name; private int rooms; private String location; boolean input(BufferedReader in) { try { System.out.println("Name: "); name = in.readLine(); System.out.println("Rooms: "); String temp = in.readLine(); rooms = to_int(temp); System.out.println("Location: "); location = in.readLine(); } catch(IOException e) { System.err.println(e); return false; } return true; } boolean write_to_dos(DataOutputStream dos) { try { dos.writeUTF(name); dos.writeInt(rooms); dos.writeUTF(location); } catch(IOException e) { System.err.println(e); return false; } return true; } boolean read_from_dis(DataInputStream dis) { try { name = dis.readUTF(); rooms = dis.readInt(); location = dis.readUTF(); } catch(EOFException e) { return false; } catch(IOException e) { System.err.println(e); return false; } return true; } void debug_print() { System.out.println("Name :" + name + ": Rooms : " + rooms + ": at :" + location+ ":"); } static int to_int(String value) { int i = 0; try { i = Integer.parseInt(value); } catch(NumberFormatException e) {} return i; } }
Serializable
marker interface can be serialized. The class has to have a
zero-parameter constructor. Also, any static and transient data
members will not be serialized.//To write FileOutputSream fos = new FileOutputStream("object.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); //YourClass is serializable YourClass yc = new YourClass(); oos.writeObject(yc); //To read. FileInputStream fis = new FileInputStream ("object.dat"); ObjectInputStream ois = new ObjectInputStream (fis); YourClass yc; yc = (YourClass) ois.readObject(); //You can also check the type of the object before casting it. FileInputStream fis = FileinputStream("object.dat"); ObjectInputStream ois = new ObjectInputStream(fis); Object object; object = ois.readObject(); if (object instanceof YourClass) { YourClass yc = object; // Perform operations with yc; } else if (object instance of AnotherClass) { AnotherClass ac = object; // Perform operations with ac }
import java.io.*; import java.util.*; public class StringTokenizerExample { public static void main(String args[]) { String line = "abc|def?ghi"; //Sets the delimeters to | and ? StringTokenizer st = new StringTokenizer(line, "|?"); while (st.hasMoreTokens()) { String s = st.nextToken(); System.out.println("Token is " + s); } } } //Output: //Token is abc //Token is def //Token is ghi
This talk available at http://jmvidal.cse.sc.edu/talks/javaio/
Copyright © 2009 José M. Vidal
.
All rights reserved.
28 May 2002, 08:39AM